import random
import time


class Queue:
    """
    队列的简单实现
    FIFO
    """

    def __init__(self, data=None):
        """
        :type data: list
        :param data: 
        """
        if data is None:
            self.data = []
            self._size = 0
            return
        self.data = data
        self._size = len(data)

    def enqueue(self, item):
        self._size += 1
        self.data.insert(0, item)

    def dequeue(self):
        if self._size > 0:
            self._size -= 1
            return self.data.pop()

    def is_empty(self):
        return self._size == 0

    def size(self):
        return self._size

    def __repr__(self):
        return "{}:{}".format(self._size, str(self.data))

    def simulate_line(self, till_show: int, max_time: int):
        """
        模拟排队购票的一个方法
        10个人排队
        距离开场的时间是till_show
        等待不超过max_time的时间就可以出队一个人
        :param max_time: 最长等待时间
        :param till_show: 距离开场还有多少秒
        """
        pq = Queue()
        tix_sold = []

        for i in range(10):
            pq.enqueue("person" + str(i))

        # 开场时间
        print("开始排队")
        now = time.time()
        t_end = now + till_show
        while now < t_end and not pq.is_empty():
            now = time.time()
            r = random.randint(0, max_time)
            time.sleep(r)
            person = pq.dequeue()
            # print(person)
            tix_sold.append(person)
        print("出票结果")
        print(tix_sold)
        return tix_sold


# usage

queue = Queue([2, 1])
print(queue)
queue.enqueue(3)
queue.enqueue(4)
queue.enqueue(5)
print(queue)

while not queue.is_empty():
    print(queue.dequeue())

# 列表解析[..]
print([i for i in range(0,10)])


# simulate_line test
queue.simulate_line(10, 3)


